import {Axios, type AxiosResponse} from "axios";
import { UserNameAndPasswordDto } from '../dto/UserNameAndPasswordDto'
import { useLocalInformationStore } from '../stores/LocalInformation'

export class MicroServiceHttpServer {
    url: string = "https://www.snowcrystal.world/"
    // url:string = "http://www.snowcrystal.world/"
    // url: string = "http://localhost";
    axios: Axios = new Axios({
       baseURL:this.url,
        withCredentials:true,
        responseType:"json",
    });

    constructor() {}

    public async getArticle(id: number) {
        return this.axios.get( `/articles/${id}`)
    }

    public async getTopNArticles(n?:number):Promise<AxiosResponse<string>> {
        return this.axios.get( "/articles/hotArticles",{
            params: {topN: n}
        })
    }

    public async getUserSimpleInformation(id:number): Promise<AxiosResponse<string>> {
        return this.axios.get(`/user/simpleInformation/${id}`)
    }
    public async getAuthenticatedUserInfo(){
        return this.axios.get("/user/this");
    }


    public async postForm(form:FormData):Promise<AxiosResponse<string>>{
        return this.axios.post("/authentication",form)
    }

    public async performLogout():Promise<AxiosResponse<string>> {
        return this.axios.post("/logout",{})
    }

    public async retrieveRecentChatroomHistory(n:number,offset:number){
        return this.axios.get( "/chatroom/default/info/history",{
            params:{
                n,
                offset
            }
        })
    }
    public  async tryEnterChatroom(fp?:string):Promise<AxiosResponse<string>> {
        return this.axios.post("/chatroom/default/entrance",{},{
            params:{
                fingerprint:fp
            }
        })
    }
    public async sendKeepAlive(fp?:string){
        return this.axios.post("/chatroom/default/keep-alive",{},{
            params:{
                fingerprint:fp
            }
        })
    }


    public async requestSendVerificationCode(email:string):Promise<AxiosResponse<string>> {
        return this.axios.post(`/user/registration/${email}`)
    }

    public async sendVerificationCode(email:string,verificationCode:string,fp?:string):Promise<AxiosResponse<string>> {
        return this.axios.post(`/user/registration/verification/${email}/${verificationCode}`,{},{
            params:{
                "fingerprint":fp
            }
        })
    }

    public async sendInPlaceModifyRequest(user:UserNameAndPasswordDto){
        return this.axios.post(`/user/modification/in-place/username-password/`,JSON.stringify(user),{
            headers:{
                'Content-Type': 'application/json'
            }
        })
    }

}

export const microServiceHttpServer = new MicroServiceHttpServer();
